/*
 * Created by JFormDesigner on Mon Jul 01 14:15:09 CST 2024
 */

package scct.ZhuZhouXing.ui;

import java.awt.event.*;

import scct.ZhuZhouXing.dao.ProductDao;
import scct.ZhuZhouXing.model.Product;

import java.awt.*;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author xzy
 */
public class PaneProductManager extends JPanel {
    public static DefaultTableModel model;
    private static ProductDao productDao = new ProductDao();
    MainFrom main;

    public PaneProductManager(MainFrom main) {
        this.main = main;
        initComponents();
        initView();//tab表提示文字
        getTable();//tab表显示数据库里面的数据
    }
//        =======================================================tab表分类表==================================================

    private void initView() {
        model = (DefaultTableModel) tabUser.getModel();
        model.addColumn("商品编号");
        model.addColumn("商品名称");
        model.addColumn("商品价格");
        model.addColumn("商品数量");
        model.addColumn("商品种类");
        model.addColumn("商品简介");
        getTable();
    }

    public static void getTable() {
        model.setRowCount(0);//删除原有数据

        List<Product> product = productDao.getProduct();
        for (Product products : product) {
            Object[] row = {products.getPid(), products.getPname(), products.getPprice(), products.getPinventory(),
                    products.getPleibie(), products.getPbrief()};
            model.addRow(row);
        }
        model.fireTableDataChanged();//刷新界面显示数据
    }

//        =======================================================查询==================================================

    private void btnSelect(ActionEvent e) {//查询
        model.setRowCount(0);//删除原有数据
        String name = txtQuery.getText();//获取搜索框里的值
        if (name.isEmpty()) {
            JOptionPane.showMessageDialog(this, "搜索框为空");
            return;
        }
        List<Product> product = productDao.getSelect(name);
        for (Product products : product) {
            model.addRow(new Object[]{
                    products.getPid(),
                    products.getPname(),
                    products.getPprice(),
                    products.getPinventory(),
                    products.getPleibie(),
                    products.getPbrief()});
        }
        model.fireTableDataChanged();//刷新界面显示数据
    }

    //        =======================================================筛选==================================================
    private void comScreen(ActionEvent e) {
        model.setRowCount(0);
        ;//删除原有数据
        String Screen = (String) comScreen.getSelectedItem();
        if (Screen == null) {
            JOptionPane.showMessageDialog(this, "没有此字段");
            return;
        }
        List<Product> product1 = productDao.getScreen(Screen);
        for (Product products : product1) {
            model.addRow(new Object[]{
                    products.getPid(),
                    products.getPname(),
                    products.getPprice(),
                    products.getPinventory(),
                    products.getPleibie(),
                    products.getPbrief()});
        }
        model.fireTableDataChanged();
    }

    private void btnAdd(ActionEvent e) {
        DlgProductAdd dlgProductAdd = new DlgProductAdd(main);
        dlgProductAdd.setModal(true);
        dlgProductAdd.setVisible(true);
    }

    // =======================================================修改商品==================================================
    private void btnUpdate(ActionEvent e) {
        if (tabUser.getSelectedRowCount() == 0) {
            JOptionPane.showMessageDialog(this, "请选择要修改的商品");
            return;
        }
        int index = tabUser.getSelectedRow();
//        System.out.println(index + "测试====");
        int id = Integer.parseInt(model.getValueAt(index, 0).toString());
//        System.out.println(id + "测试====");
        DlgProductupdate dlgProductupdate = new DlgProductupdate(main, id);
        dlgProductupdate.setModal(true);
        dlgProductupdate.setVisible(true);
    }

    // =======================================================删除商品==================================================
    private void btndelect(ActionEvent e) {
        if (tabUser.getSelectedRowCount() == 0) {
            JOptionPane.showMessageDialog(this, "请选择要删除的商品！");
            return;
        }
        int index = tabUser.getSelectedRow();
//        System.out.println(index + "测试====");
        int id = Integer.parseInt(model.getValueAt(index, 0).toString());
//        System.out.println(id + "测试====");
        DljProductDele dljProductDele = new DljProductDele(main, id);
        dljProductDele.setModal(true);
        dljProductDele.setVisible(true);

    }
    // =======================================================重置==================================================

    private void btnReset(ActionEvent e) {
        getTable();
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - 老登
        panel1 = new JPanel();
        btnAdd = new JButton();
        btnUpdate = new JButton();
        btndelect = new JButton();
        btnReset = new JButton();
        scrollPane1 = new JScrollPane();
        tabUser = new JTable();
        panel2 = new JPanel();
        txtQuery = new JTextField();
        btnSelect = new JButton();
        comScreen = new JComboBox<>();
        label1 = new JLabel();

        //======== this ========
        setBorder(new javax.swing.border.CompoundBorder(new javax.swing.border.TitledBorder(new
        javax.swing.border.EmptyBorder(0,0,0,0), "JF\u006frm\u0044es\u0069gn\u0065r \u0045va\u006cua\u0074io\u006e",javax
        .swing.border.TitledBorder.CENTER,javax.swing.border.TitledBorder.BOTTOM,new java
        .awt.Font("D\u0069al\u006fg",java.awt.Font.BOLD,12),java.awt
        .Color.red), getBorder())); addPropertyChangeListener(new java.beans.
        PropertyChangeListener(){@Override public void propertyChange(java.beans.PropertyChangeEvent e){if("\u0062or\u0064er".
        equals(e.getPropertyName()))throw new RuntimeException();}});
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.LEFT));

            //---- btnAdd ----
            btnAdd.setText("\u589e\u52a0\u5546\u54c1");
            btnAdd.addActionListener(e -> btnAdd(e));
            panel1.add(btnAdd);

            //---- btnUpdate ----
            btnUpdate.setText("\u4fee\u6539\u5546\u54c1");
            btnUpdate.addActionListener(e -> btnUpdate(e));
            panel1.add(btnUpdate);

            //---- btndelect ----
            btndelect.setText("\u5220\u9664\u5546\u54c1");
            btndelect.addActionListener(e -> btndelect(e));
            panel1.add(btndelect);

            //---- btnReset ----
            btnReset.setText("\u5237\u65b0\u9875\u9762");
            btnReset.addActionListener(e -> btnReset(e));
            panel1.add(btnReset);
        }
        add(panel1, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {

            //---- tabUser ----
            tabUser.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            scrollPane1.setViewportView(tabUser);
        }
        add(scrollPane1, BorderLayout.CENTER);

        //======== panel2 ========
        {
            panel2.setLayout(null);

            //---- txtQuery ----
            txtQuery.setToolTipText("name");
            panel2.add(txtQuery);
            txtQuery.setBounds(295, 0, 230, txtQuery.getPreferredSize().height);

            //---- btnSelect ----
            btnSelect.setText("\u67e5\u8be2\u5546\u54c1");
            btnSelect.addActionListener(e -> btnSelect(e));
            panel2.add(btnSelect);
            btnSelect.setBounds(new Rectangle(new Point(525, 0), btnSelect.getPreferredSize()));

            //---- comScreen ----
            comScreen.setModel(new DefaultComboBoxModel<>(new String[] {
                "\u98df\u54c1\u96f6\u98df",
                "\u996e\u7528\u77ff\u6cc9\u6c34",
                "\u5bb6\u7528\u7535\u5668",
                "\u6d17\u6f31\u7528\u54c1",
                "\u852c\u83dc\u679c\u852c",
                "\u6d77\u9c9c\u4ea7\u54c1",
                "\u8c03\u5473\u54c1",
                "\u7535\u5b50\u4ea7\u54c1"
            }));
            comScreen.addActionListener(e -> comScreen(e));
            panel2.add(comScreen);
            comScreen.setBounds(new Rectangle(new Point(0, 0), comScreen.getPreferredSize()));

            //---- label1 ----
            label1.setText("\u8bf7\u8f93\u5165\u9700\u8981\u67e5\u8be2\u7684\u5546\u54c1\u540d\u79f0");
            panel2.add(label1);
            label1.setBounds(115, 0, 175, 30);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel2.getComponentCount(); i++) {
                    Rectangle bounds = panel2.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel2.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel2.setMinimumSize(preferredSize);
                panel2.setPreferredSize(preferredSize);
            }
        }
        add(panel2, BorderLayout.SOUTH);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - 老登
    private JPanel panel1;
    private JButton btnAdd;
    private JButton btnUpdate;
    private JButton btndelect;
    private JButton btnReset;
    private JScrollPane scrollPane1;
    private JTable tabUser;
    private JPanel panel2;
    private JTextField txtQuery;
    private JButton btnSelect;
    private JComboBox<String> comScreen;
    private JLabel label1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
